#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;

using NUnit.Framework;

using octalforty.Componento.ComponentModel.Installation;

using Rhino.Mocks;

namespace octalforty.Componento.Tests.Componento.ComponentModel.Installation
{
    [TestFixture()]
    public class ResourceInstallerServiceTestFixture
    {
        [Test()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ResolveResourceInstallerThrowsArgumentNullExceptionOnNullUri()
        {
            new ResourceInstallerService().ResolveResourceInstaller(null);
        }

        [Test()]
        public void ResolveResourceInstallerReturnsNullWhenNoResourceInstallersRegistered()
        {
            Assert.IsNull(new ResourceInstallerService().ResolveResourceInstaller(new Uri("http://foo")));
        }

        [Test()]
        public void ResolveResourceInstallerResolvesWithPriority()
        {
            MockRepository mockRepository = new MockRepository();

            IResourceInstaller priority1Installer = mockRepository.StrictMock<IResourceInstaller>();
            IResourceInstaller priority50Installer = mockRepository.StrictMock<IResourceInstaller>();
            IResourceInstaller priority10Installer = mockRepository.StrictMock<IResourceInstaller>();

            Uri uri = new Uri("http://foo");

            using (mockRepository.Ordered())
            {
                Expect.Call(priority50Installer.CanInstallResourceFrom(uri)).Return(false);
                Expect.Call(priority10Installer.CanInstallResourceFrom(uri)).Return(false);
                Expect.Call(priority1Installer.CanInstallResourceFrom(uri)).Return(false);
            } // using

            ResourceInstallerService resourceInstaller = new ResourceInstallerService();
            resourceInstaller.ResourceInstallers =
                new Lazy<IResourceInstaller, IResourceInstallerMetadata>[]
                    {
                        new Lazy<IResourceInstaller, IResourceInstallerMetadata>(
                            delegate { return priority1Installer; }, new ResourceInstallerAttribute(1)), 
                        new Lazy<IResourceInstaller, IResourceInstallerMetadata>(
                            delegate { return priority50Installer; }, new ResourceInstallerAttribute(50)), 
                        new Lazy<IResourceInstaller, IResourceInstallerMetadata>(
                            delegate { return priority10Installer; }, new ResourceInstallerAttribute(10)), 
                    };

            mockRepository.ReplayAll();

            resourceInstaller.ResolveResourceInstaller(uri);
        }

        [Test()]
        public void InstallResources()
        {
            MockRepository mockRepository = new MockRepository();
            IResourceInstaller zipResourceInstaller = mockRepository.StrictMock<IResourceInstaller>();
            IResourceInstaller tarResourceInstaller = mockRepository.StrictMock<IResourceInstaller>();

            Uri fooZipUri = new Uri("file:///d:/foo.zip");
            Uri fooTarUri = new Uri("file:///d:/foo.tar");

            using(mockRepository.Record())
            {
                Expect.Call(zipResourceInstaller.CanInstallResourceFrom(fooZipUri)).
                    Return(true);

                Expect.Call(delegate { zipResourceInstaller.InstallResource(fooZipUri, @"e:\lib\foo", null); });

                Expect.Call(zipResourceInstaller.CanInstallResourceFrom(fooTarUri)).
                    Return(false);

                Expect.Call(tarResourceInstaller.CanInstallResourceFrom(fooTarUri)).
                    Return(true);

                Expect.Call(delegate { tarResourceInstaller.InstallResource(fooTarUri, @"e:\lib\foo", null); });
            } // using

            ResourceInstallerService resourceInstaller = new ResourceInstallerService();
            resourceInstaller.ResourceInstallers = 
                new Lazy<IResourceInstaller, IResourceInstallerMetadata>[]
                    {
                        new Lazy<IResourceInstaller, IResourceInstallerMetadata>(
                            delegate { return zipResourceInstaller; }, new ResourceInstallerAttribute(100)), 
                        new Lazy<IResourceInstaller, IResourceInstallerMetadata>(
                            delegate { return tarResourceInstaller; }, new ResourceInstallerAttribute(10)), 
                    };

            resourceInstaller.InstallResources(new Uri[] { fooZipUri, fooTarUri }, @"e:\lib\foo", null);
        }
    }
}
